﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Xceed.Wpf.Toolkit.PropertyGrid;

namespace CPPEI.Coolzon.UI.XProperty.Editors
{
    /// <summary>
    /// Interaction logic for XDoubleEditor.xaml
    /// </summary>
    public partial class XDoubleEditor : XPropertyValueEditor
    {
        public static DependencyProperty PropertyValueProperty = DependencyProperty.Register("PropertyValue", typeof(double), typeof(XDoubleEditor), new PropertyMetadata(propertyChangedCallback));
        private static void propertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            XDoubleEditor editor = d as XDoubleEditor;
            if (e.NewValue != null)
            {
                editor._ctlEditValue.Text = e.NewValue.ToString();
            }
            else
            {
                editor._ctlEditValue.Text = "";
            }
        }
        protected override void OnXInit(PropertyItem propertyItem)
        {
            Binding binding = new Binding();
            binding.Source = propertyItem;
            binding.Path = new PropertyPath("Value");
            binding.Mode = BindingMode.TwoWay;
            this.SetBinding(PropertyValueProperty, binding);
        }
        protected override DependencyProperty GetPropertyValue()
        {
            return PropertyValueProperty;
        }

        public XDoubleEditor()
        {
            InitializeComponent();
            
            this._ctlEditValue.TextInput += _ctlEditValue_TextInput;
            this._ctlEditValue.LostFocus += _ctlEditValue_LostFocus;
        }


        void _ctlEditValue_TextInput(object sender, TextCompositionEventArgs e)
        {
            e.Handled =true;
        }

        #region implement IPropertyValueEditor

        protected override void OnInitXPropertyValue(XPropertyAttribute xProperty, PropertyInfo propertyInfo, object value)
        {
        }

        protected override void OnXInit(Part partInstance, XPropertyAttribute xProperty, PropertyInfo propertyInfo, object value)
        {
            Binding binding = new Binding();
            binding.Source = partInstance;
            binding.Path = new PropertyPath(propertyInfo.Name);
            this.SetBinding(PropertyValueProperty, binding);
        }

        private void _ctlEditValue_LostFocus(object sender, RoutedEventArgs e)
        {
            ValueChanged();
        }

        private void _ctlEditValue_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if(e.Key == Key.Enter)
            {
                ValueChanged();
            }
        }

        private void ValueChanged()
        {
            double d = 0;
            if (double.TryParse(_ctlEditValue.Text, out d))
            {
                UpdateXPropertyValue(d);
            }
        }

        #endregion

        protected override void OnUnDo(object oldValue)
        {
            base.OnUnDo(oldValue);

            _ctlEditValue.Text = Convert.ToString(oldValue);
        }
    }
}
